package cn.com.woxitech.dev.controller.api;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;

import cn.com.woxitech.dev.common.APIResultFactory;
import cn.com.woxitech.dev.common.JSONUtils;
import cn.com.woxitech.dev.model.gen.WmLecture;
import cn.com.woxitech.dev.model.gen.WmLecturePage;
import cn.com.woxitech.dev.model.gen.WmRoom;
import cn.com.woxitech.dev.service.AppService;
import cn.com.woxitech.dev.service.CatalogService;
import cn.com.woxitech.dev.service.LecturePageService;
import cn.com.woxitech.dev.service.LectureService;
import cn.com.woxitech.dev.service.OrganizationService;
import cn.com.woxitech.dev.service.PrivilegeService;
import cn.com.woxitech.dev.service.QiniuService;
import cn.com.woxitech.dev.service.RoomService;
import cn.com.woxitech.dev.service.SMSCodeService;
import cn.com.woxitech.dev.service.SettingService;
import cn.com.woxitech.dev.service.UserService;
import cn.com.woxitech.dev.service.WeixinService;

@RestController
@RequestMapping("/luyan")
public class StreamLUYANController {
	@Autowired
	WeixinService weixinService;
	@Autowired
	SettingService settingService;
	@Autowired
	PrivilegeService privilegeService;
	@Autowired
	QiniuService qiniuService;
	@Autowired
	RoomService roomService;
	@Autowired
	LectureService lectureService;
	@Autowired
	LecturePageService lecturePageService;
	@Autowired
	UserService userService;
	@Autowired
	SMSCodeService smsCodeService;
	@Autowired
	OrganizationService organizationService;
	@Autowired
	CatalogService catalogService;
	@Autowired
	AppService appService;
	@Autowired
	private Environment env;

	

	
	@RequestMapping(value="/stream/record",method=RequestMethod.POST)
	public Object streamrecord(@RequestBody String body) throws JsonProcessingException, IOException {
		try {
			Map<String, Object> mp = new HashMap<String, Object>();
			
			JsonNode jsonNode = JSONUtils.toJson(body);
			
			long start = 0;
			long end = 0;
			
			String roomuuid = null;
			WmRoom room = null;
			
			if(jsonNode.has("roomuuid")){
				roomuuid = jsonNode.get("roomuuid").asText();
			}
			
			if(roomuuid != null){
				room = this.roomService.getRoomsByUuid(roomuuid);
				start = room.getTime().getTime();
			}
			
			String streamid = jsonNode.get("streamid").asText();
			
			if(jsonNode.has("start")){
				start = jsonNode.get("start").asLong() / 1000;
			}
			
			if(jsonNode.has("end")){
				end = jsonNode.get("end").asLong() / 1000;
			}
			String playbackurl = qiniuService.savePlaybackRecord(streamid, start, end);
			
			if(roomuuid !=null && playbackurl != null){

				WmLecture lecture = this.lectureService.getLectureByRoomUuid(roomuuid);
				
				if(room.getLiveType().equals("1")){//视频
					WmLecturePage lp = this.lecturePageService.getFirstLecturePageByLectureUuid(lecture.getUuid());
					lp.setPageurl(playbackurl);
					room.setType("3");
					this.lecturePageService.updateByPrimaryKey(lp);
					this.roomService.updateByPrimaryKey(room);
					
				}else if(room.getLiveType().equals("2")){//smalllive
					
					List<WmLecturePage> lpList = this.lecturePageService.getLecturePageByLectureUuid(lecture.getUuid());
					for(int i=0;i<lpList.size();i++){
						if(lpList.get(i).getType().equals("smalllive")){
							WmLecturePage lp = lpList.get(i);
							lp.setPageurl(playbackurl);
							room.setType("3");
							this.lecturePageService.updateByPrimaryKey(lp);
							this.roomService.updateByPrimaryKey(room);
							break;
						}
						
					}
					
				}

			}
			
			mp.put("playbackurl", playbackurl);
			return APIResultFactory.success(mp);	
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			return APIResultFactory.fail();	
		}
	}
	
	@RequestMapping(value="/stream/room/{roomuuid}/switch",method=RequestMethod.POST)
	public Object stream_room_switch(@PathVariable String roomuuid,@RequestBody String body) {
		JsonNode jsonNode;
		Map<String, Object> mp = new HashMap<String,Object>();
		try {
			
			jsonNode = JSONUtils.toJson(body);
			String streamid = jsonNode.get("streamid").asText();
			String coverurl = jsonNode.get("coverurl").asText();

			WmLecture lecture = this.lectureService.getLectureByRoomUuid(roomuuid);
			WmLecturePage page = lecturePageService.getFirstLecturePageByLectureUuid(lecture.getUuid());
			
			
			if(streamid.equals("")){//更新coverurl, 预览
				page.setThumburl(coverurl);
			}else{
				
				Map<String, String> streamdata = qiniuService.getStreamUrlsByStreamId(streamid);
				
				page.setPublishid(streamid);
				
				page.setPublishurl(streamdata.get("publishurl"));
				page.setThumburl(coverurl);

				int status = qiniuService.getStreamStatus(streamid);
				
				if(status == 0){//表明有回放
					page.setPageurl(streamdata.get("recordurl"));
				}else{
					page.setPageurl(streamdata.get("playurl"));
				}
				
			}
			lecturePageService.updateByPrimaryKey(page);

			return APIResultFactory.success();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return APIResultFactory.fail();
	}
	
	
	@RequestMapping(value="/stream/status",method=RequestMethod.POST)
	public Object stream_status(@RequestBody String body) {
		JsonNode jsonNode;
		String streamid = "";//"z1.haojslive.57cf97f6b1d1ff829c047712";
		Map<String, Object> mp = new HashMap<String,Object>();
		try {
			
			jsonNode = JSONUtils.toJson(body);
			String publishid = jsonNode.get("publishid").asText();
			streamid = publishid; 
			Map<String, String> streamdata = qiniuService.getStreamUrlsByStreamId(streamid);
			mp.put("recordurl", streamdata.get("recordurl"));
			mp.put("publishurl", streamdata.get("publishurl"));
			mp.put("playurl", streamdata.get("playurl"));
			mp.put("rtmpurl", streamdata.get("rtmpurl"));
			mp.put("status", qiniuService.getStreamStatus(streamid));
			return APIResultFactory.success(mp);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return APIResultFactory.fail();
	}
}
